//
//  DirectoryDetailViewController.m
//  DSNDialer
//
//  Created by Chris Morrell on 4/30/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "DirectoryDetailViewController.h"

@implementation DirectoryDetailViewController
@synthesize siteNameLabel;
@synthesize locationLabel;
@synthesize dsnNumberLabel;
@synthesize commNumberLabel;
@synthesize dialNumberButton;
@synthesize thisPhoneBookEntry;
@synthesize thisPhoneBookDictionary;
@synthesize saveButton;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	self.view.backgroundColor = [UIColor clearColor];
	
	UIImage *buttonImageNormal = [UIImage imageNamed:@"test-gradient.png"];
	UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
	UIImage *buttonImagePressed = [UIImage imageNamed:@"test-gradient2.png"];
	UIImage *stretchableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
	
	[dialNumberButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal];
	[dialNumberButton setBackgroundImage:stretchableButtonImagePressed forState:UIControlStateHighlighted];
	[dialNumberButton setAlpha:0.85];
	
	[saveButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal];
	[saveButton setBackgroundImage:stretchableButtonImagePressed forState:UIControlStateHighlighted];
	[saveButton setAlpha:0.85];
	
	PhoneBookEntry *newPhoneBookEntry = [[PhoneBookEntry alloc] init];
	newPhoneBookEntry.sitename = [thisPhoneBookDictionary objectForKey:@"sitename"];
	newPhoneBookEntry.dsn = [thisPhoneBookDictionary objectForKey:@"dsn"];
	newPhoneBookEntry.comm = [thisPhoneBookDictionary objectForKey:@"comm"];
	newPhoneBookEntry.city = [thisPhoneBookDictionary objectForKey:@"city"];
	newPhoneBookEntry.state = [thisPhoneBookDictionary objectForKey:@"state"];
	newPhoneBookEntry.country = [thisPhoneBookDictionary objectForKey:@"country"];
	self.thisPhoneBookEntry = newPhoneBookEntry;
	[newPhoneBookEntry release];
	
	siteNameLabel.text = thisPhoneBookEntry.sitename;
	
	if ([thisPhoneBookEntry.city isEqualToString:@""] && [thisPhoneBookEntry.state isEqualToString:@""] && [thisPhoneBookEntry.country isEqualToString:@""]) 
	{
		locationLabel.text = @"";
	}
	else if ([thisPhoneBookEntry.city isEqualToString:@""] && [thisPhoneBookEntry.state isEqualToString:@""])
	{
		locationLabel.text = [NSString stringWithFormat:@"%@",thisPhoneBookEntry.country];
	}
	else if ([thisPhoneBookEntry.state isEqualToString:@""] && [thisPhoneBookEntry.country isEqualToString:@""])
	{
		locationLabel.text = [NSString stringWithFormat:@"%@",thisPhoneBookEntry.city];
	}
	else if ([thisPhoneBookEntry.city isEqualToString:@""] && [thisPhoneBookEntry.country isEqualToString:@""])
	{
		locationLabel.text = [NSString stringWithFormat:@"%@",thisPhoneBookEntry.state];
	}
	else if ([thisPhoneBookEntry.city isEqualToString:@""])
	{
		locationLabel.text = [NSString stringWithFormat:@"%@, %@",thisPhoneBookEntry.state,thisPhoneBookEntry.country];
	}
	else if ([thisPhoneBookEntry.state isEqualToString:@""])
	{
		locationLabel.text = [NSString stringWithFormat:@"%@, %@",thisPhoneBookEntry.city,thisPhoneBookEntry.country];
	}
	else if ([thisPhoneBookEntry.country isEqualToString:@""])
	{
		locationLabel.text = [NSString stringWithFormat:@"%@, %@",thisPhoneBookEntry.city,thisPhoneBookEntry.state];
	}
	else 
	{
		locationLabel.text = [NSString stringWithFormat:@"%@, %@, %@", thisPhoneBookEntry.city, thisPhoneBookEntry.state, thisPhoneBookEntry.country];
	}
	dsnNumberLabel.text = thisPhoneBookEntry.dsn;
	commNumberLabel.text = thisPhoneBookEntry.comm;
}

-(IBAction)dialNumberButtonAction:(id)sender
{
	if ([thisPhoneBookEntry.comm isEqualToString:@""])
	{
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:@"%@ has no number to dial.",thisPhoneBookEntry.sitename] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
	else 
	{
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%@",thisPhoneBookEntry.comm] message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Call",nil];
		[alert show];
		[alert release];
	}
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
	if (alertView.numberOfButtons == 2 && buttonIndex == 1)
	{
		NSString *numberToDial = [NSString stringWithFormat:@"tel:%@",thisPhoneBookEntry.comm];
		[[UIApplication sharedApplication] openURL:[NSURL URLWithString:numberToDial]];
	}
}

-(IBAction)saveButtonAction:(id)sender
{
	ABNewPersonViewController *addPersonViewController = [[ABNewPersonViewController alloc] init];
	addPersonViewController.newPersonViewDelegate = self;
	
	ABRecordRef newContact = ABPersonCreate();
	CFErrorRef anError = NULL;
	
	ABRecordSetValue(newContact, kABPersonOrganizationProperty, thisPhoneBookEntry.sitename, &anError);
	
	ABMultiValueRef commercialNumber = ABMultiValueCreateMutable(kABMultiStringPropertyType);
	if (ABMultiValueAddValueAndLabel(commercialNumber, thisPhoneBookEntry.comm, kABPersonPhoneMainLabel, NULL) && ![thisPhoneBookEntry.comm isEqualToString:@""])
	{
		ABRecordSetValue(newContact, kABPersonPhoneProperty, commercialNumber, &anError);
		
		addPersonViewController.displayedPerson = newContact;
		addPersonViewController.view.backgroundColor = [UIColor clearColor];
		for (UIView *thisView in addPersonViewController.view.subviews)
		{
			thisView.backgroundColor = [UIColor clearColor];
		}		
		
		UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:addPersonViewController];
		navigation.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
		navigation.navigationBar.tintColor = [UIColor grayColor];
		navigation.view.backgroundColor = [UIColor clearColor];
		[self presentModalViewController:navigation animated:YES];
		[navigation release];
	}
	else
	{
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:@"Can't create a contact for %@",thisPhoneBookEntry.sitename] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
	CFRelease(newContact);
	CFRelease(commercialNumber);
	[addPersonViewController release];
}

- (void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person
{
	[self dismissModalViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
	[thisPhoneBookEntry release];
	[thisPhoneBookDictionary release];
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	self.siteNameLabel = nil;
	self.locationLabel = nil;
	self.dsnNumberLabel = nil;
	self.commNumberLabel = nil;
	self.dialNumberButton = nil;
	self.saveButton = nil;
}


- (void)dealloc {
   [super dealloc];
	[siteNameLabel release];
	[locationLabel release];
	[dsnNumberLabel release];
	[commNumberLabel release];
	[dialNumberButton release];
	[saveButton release];
	[thisPhoneBookEntry release];
	[thisPhoneBookDictionary release];
}


@end
